package com.fearless.demo04;

import com.fearless.demo04.impl.UserServiceImpl;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Date;

public class ProxyTest {
    public static void main(String[] args) {
        UserService userService=new UserServiceImpl();
        MyInvocationHandler mih=new MyInvocationHandler();
        mih.setTarget(userService);

        UserService proxyInstance = (UserService) Proxy.newProxyInstance(userService.getClass().getClassLoader(),
                userService.getClass().getInterfaces(),
                mih);

        proxyInstance.add();//会调用InvocationHandle中的invoke()
    }


}
class MyInvocationHandler implements InvocationHandler{

        Object target;//目标类

        public void setTarget(Object target) {
            this.target = target;
        }


        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

            System.out.println("功能增强啊哦哦啊哦");
            Object result = method.invoke(target, args);//目标方法

            System.out.println(new Date());

            return null;
        }
}